package cz.muni.fi.pa165.hauntedhouses.rest.web;

import cz.muni.fi.pa165.hauntedhouses.dto.AbilityDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.GhostDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.HouseDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.HumanDTO;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

/**A bean for play feature of our application which manipulates with all entities (ghosts, humans, houses and abilities)
 *
 * @author Jarda Benda
 */
@UrlBinding("/jsp/play")
public class PlayActionBean implements ActionBean {
    final static String URI="http://localhost:8080/pa165/rest";
    private ActionBeanContext context;
    
    @SpringBean
    RestTemplate restTemplate;

    public HttpHeaders getHttpHeader(){
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("Authorization", "Basic rest:rest");
        return requestHeaders;
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }   
    
    public List<GhostDTO> getGhosts() {
        GhostDTO[] ghosts = restTemplate.getForObject(URI+"/ghosts", GhostDTO[].class);
        return (Arrays.asList(ghosts));
    }

    public List<AbilityDTO> getAbilities() {     
        AbilityDTO[] ablts = restTemplate.getForObject(URI+"/abilities?ghost="+ghost.getId(), AbilityDTO[].class);
        return (Arrays.asList(ablts));
    }

    public List<HumanDTO> getHumans() {
        HumanDTO[] hmns = restTemplate.getForObject(URI+"/humans?house="+house.getId(), HumanDTO[].class);
        return (Arrays.asList(hmns));
    }

    public List<HouseDTO> getHouses() {
        HouseDTO[] houses = restTemplate.getForObject(URI+"/houses", HouseDTO[].class);
        return (Arrays.asList(houses));
    }
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play2"}, field = "id", required = true),
    })
    private HouseDTO house;
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play3"}, field = "id", required = true),
    })
    private HumanDTO human;
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play2","play3"}, field = "id", required = true),
    })
    private GhostDTO ghost;
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play3"}, field = "id", required = true),
    })
    private AbilityDTO ability;
    
    private int diffExp;
    private int diffPsyche;
    private int diffLvl;

    public int getDiffLvl() {
        return diffLvl;
    }

    public void setDiffLvl(int diffLvl) {
        this.diffLvl = diffLvl;
    }

    public int getDiffExp() {
        return diffExp;
    }

    public void setDiffExp(int diffExp) {
        this.diffExp = diffExp;
    }

    public int getDiffPsyche() {
        return diffPsyche;
    }

    public void setDiffPsyche(int diffPsyche) {
        this.diffPsyche = diffPsyche;
    }

    public HouseDTO getHouse() {
        return house;
    }

    public void setHouse(HouseDTO house) {
        this.house = house;
    }

    public HumanDTO getHuman() {
        return human;
    }

    public void setHuman(HumanDTO human) {
        this.human = human;
    }

    public GhostDTO getGhost() {
        return ghost;
    }

    public void setGhost(GhostDTO ghost) {
        this.ghost = ghost;
    }

    public AbilityDTO getAbility() {
        return ability;
    }

    public void setAbility(AbilityDTO ability) {
        this.ability = ability;
    }
    
    @DefaultHandler
    public Resolution play() {
        return new ForwardResolution("/jsp/play.jsp");
    }

    public Resolution play2() {
        return new ForwardResolution("/jsp/play2.jsp");
    }

    public Resolution play3() {
        int exp = ghost.getExperience();
        int psych = human.getPsyche();
        int lvl = ghost.getLvl();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("event", "haunt");
        map.put("ghost", ghost);
        map.put("ability", ability);
        map.put("human", human);
        restTemplate.postForObject(URI, map, Map.class);
        ghost = restTemplate.getForObject(URI+"/ghost?id="+ghost.getId(), GhostDTO.class);
        human = restTemplate.getForObject(URI+"/human?id="+human.getId(), HumanDTO.class);
        diffExp = ghost.getExperience() - exp;
        diffPsyche = human.getPsyche() - psych;
        diffLvl = ghost.getLvl() - lvl;
        return new ForwardResolution("/jsp/play3.jsp");
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play2","play3"})
    public void loadGhostFromServer() {
        String ids = context.getRequest().getParameter("ghost.id");
        if (ids == null) {
            return;
        }
        GhostDTO ghst = restTemplate.getForObject(URI+"/ghost?id="+ids, GhostDTO.class);
        if (ghst == null) {
            return;
        }
        ghost=ghst;
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play2","play3"})
    public void loadHouseFromServer() {
        String ids = context.getRequest().getParameter("house.id");
        if (ids == null) {
            return;
        }
        HouseDTO hse = restTemplate.getForObject(URI+"/house?id="+ids, HouseDTO.class);
        if (hse == null) {
            return;
        }
        house=hse;
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play3"})
    public void loadAbilityFromServer() {
        String ids = context.getRequest().getParameter("ability.id");
        if (ids == null) {
            return;
        }
        AbilityDTO ablt = restTemplate.getForObject(URI+"/ability?id="+ids, AbilityDTO.class);
        if (ablt == null) {
            return;
        }
        ability=ablt;
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play3"})
    public void loadHumanFromServer() {
        String ids = context.getRequest().getParameter("human.id");
        if (ids == null) {
            return;
        }
        HumanDTO hmn = restTemplate.getForObject(URI+"/human?id="+ids, HumanDTO.class);
        if (hmn == null) {
            return;
        }
        human=hmn;
    }
}
